package com.efast.dmom.mgmt.controller.mes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.dmom.mgmt.bean.mes.ProductExcuteDto;
import com.efast.dmom.mgmt.entity.mes.TrBasProductCfgBi;
import com.efast.dmom.mgmt.entity.mes.TrBasProductCfgDci;
import com.efast.dmom.mgmt.entity.mes.TrBasProductCfgRepaircode;
import com.efast.dmom.mgmt.entity.mes.TrBasProductCfgTestitem;
import com.efast.dmom.mgmt.entity.mes.TrBasProductConfig;
import com.efast.dmom.mgmt.entity.mes.TwBasLot;
import com.efast.dmom.mgmt.entity.mes.TwBasLotRepair;
import com.efast.dmom.mgmt.service.mes.TrBasProductCfgBiService;
import com.efast.dmom.mgmt.service.mes.TrBasProductCfgDciService;
import com.efast.dmom.mgmt.service.mes.TrBasProductCfgRepaircodeService;
import com.efast.dmom.mgmt.service.mes.TrBasProductCfgTestitemService;
import com.efast.dmom.mgmt.service.mes.TrBasProductConfigService;
import com.efast.dmom.mgmt.service.mes.TwBasLotRepairService;
import com.efast.dmom.mgmt.utils.Assist;

@Controller
@RequestMapping(value="/trBasProductCfgBi")
public class TrBasProductCfgBiController {
	@Autowired
	private TrBasProductCfgBiService trBasProductCfgBiService;
	@Autowired
	private TrBasProductConfigService trBasProductConfigService;
	@Autowired
	private TrBasProductCfgTestitemService trBasProductCfgTestitemService;
	@Autowired
	private TrBasProductCfgDciService trBasProductCfgDciService;
	@Autowired
	private TwBasLotRepairService twBasLotRepairService;
	@Autowired
	private TrBasProductCfgRepaircodeService trBasProductCfgRepaircodeService;
	
	//校验生产过程定义配置数量并返回配置信息
	@RequestMapping(value = "/checkProductConfig")
	public @ResponseBody ResultBean checkProductConfig(@RequestBody ProductExcuteDto dto){
		ResultBean result=new ResultBean(true,"'");
		try {
			TwBasLot lot = dto.getRowData();
			Assist assist = new Assist();
			assist.setRequires(Assist.andEq("part_no_version", lot.getPartNoVersion()));
			assist.setRequires(Assist.andEq("route_name", lot.getRoute()));
			assist.setRequires(Assist.andEq("operation_id", lot.getOperationId()));
			assist.setRequires(Assist.andEq("station_id", lot.getStationId()));
			List<TrBasProductConfig> cfgList =trBasProductConfigService.selectTrBasProductConfig(assist);
			if(cfgList!=null&&cfgList.size()>0) {
				if(cfgList.size()>1) {
					result.setSuccess(false);
					result.setMessage("此零件包含"+cfgList.size()+"条过程定义，请先检查零件过程定义!");
				}else {
					result.setObject(cfgList.get(0));
				}
			}else {
				result.setSuccess(false);
				result.setMessage("请先配置零件过程定义!");
			}
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
		}
		return result;
	}
	
	//获取生产过程定义中对应工序下的，Bom Item物料消耗配置、校验项配置、数据采集配置、维修配置
	@RequestMapping(value = "/getAllDetailData")
	public @ResponseBody ResultBean getAllDetailData(@RequestBody Map<Object,Object> map){
		ResultBean result=new ResultBean(true,"'");
		Map<String,Object> dataMap = new HashMap<String,Object>();
		Integer lotId = (Integer)map.get("lotId");
		Integer operationId = (Integer)map.get("operationId");
		Integer productConfigId = (Integer)map.get("productConfigId");
		//获取产品过程定义的消耗配置
		List<TrBasProductCfgBi> cfgBi =trBasProductCfgBiService.selectTrBasProductCfgBiAndPn(lotId,productConfigId,operationId);
		dataMap.put("cfgBi", cfgBi);
		//获取产品过程定义的校验项配置
		List<TrBasProductCfgTestitem> cfgTest = trBasProductCfgTestitemService.selectLotProductCfgTestitem(lotId,productConfigId,operationId);
		dataMap.put("cfgTest", cfgTest);
		//获取产品过程定义的数据采集配置
		List<TrBasProductCfgDci> cfgDci = trBasProductCfgDciService.selectLotProductCfgDci(lotId,productConfigId,operationId);
		dataMap.put("cfgDci", cfgDci);
		//判断指定工序是否定义了维修数据
		List<TrBasProductCfgRepaircode> cfgRep = trBasProductCfgRepaircodeService.selectTrBasProductCfgRepaircode(
				new Assist().setRequires(
						Assist.andEq("product_config_id", productConfigId)
						));
		if(cfgRep!=null&&cfgRep.size()>0) {
			dataMap.put("cfgRep", true);
		}else {
			dataMap.put("cfgRep", false);
		}
		//获取已保存的维修数据，若无则显示空
		List<TwBasLotRepair> lotRepair = twBasLotRepairService.selectTwBasLotRepair(
				new Assist().setRequires(
						Assist.andEq("lot_id", lotId),
						Assist.andEq("operation_id", operationId)
					));
		if(lotRepair!=null&&lotRepair.size()>0) {
			for(int i=0;i<lotRepair.size();i++) {
				lotRepair.get(i).setDefectCode(lotRepair.get(i).getDefectCodeId()+"_"+lotRepair.get(i).getDefectCode());
				lotRepair.get(i).setRepairCode(lotRepair.get(i).getRepairCodeId()+"_"+lotRepair.get(i).getRepairCode());
			}
		}
		dataMap.put("lotRepair", lotRepair);
		result.setObject(dataMap);
		return result;
	}
	
	@RequestMapping(value = "/getOperationIdByStationName/{stationId}")
	public @ResponseBody ResultBean getOperationIdByStationName(@PathVariable("stationId") Integer stationId){
		ResultBean result=new ResultBean(true,"'");
		
		result.setObject(trBasProductConfigService.getOperationIdByStationName(stationId));
		
		return result;
	}
	
	@RequestMapping(value = "/getAttachmentId/{routeId}/{operationId}")
	public @ResponseBody ResultBean getAttachmentId(@PathVariable("routeId") Integer routeId, @PathVariable("operationId") Integer operationId){
		
		ResultBean result=new ResultBean(true,null);
		result.setObject(trBasProductConfigService.getAttachId(routeId, operationId));
		
		return result;
	}
	
	@RequestMapping(value = "/getFormIdByProductConfigId/{productConfigId}")
	public @ResponseBody ResultBean getFormIdByProductConfigId(@PathVariable("productConfigId") Integer productConfigId){
		
		ResultBean result=new ResultBean(true,null);
		result.setObject(trBasProductConfigService.getFormIdByProductConfigId(productConfigId));
		
		return result;
	}
}
